<template>
  <div class="content1">
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
      </div>
    </div>
    <div>
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
        serial
      >
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel1(col.scope.row.fnumber)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
      </jc-table>
      <cardTab field="fnumber" @search="handleQueryBomList" />
      <!--    分页器-->
      <div class="footer">
        <jc-pagination
          :total="total"
          :page.sync="currentPage"
          :limit.sync="size"
          @pagination="handleGetUntreated"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryCostSubInstock
} from '@/api/financial/salesMargin'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  // name: 'ForthwithRep',
  components: {
    cardTab: () => import('@/components/cardTab'),
    search
  },
  mixins: [searData],
  data() {
    return {
      ftype: 92,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeight: '73vh',
      tableHeader: [
        { label: this.$t('market.paezText1'), prop: 'sal_khddh', width: '120' },
        { label: this.$t('market.period'), prop: 'period', width: '100' },
        { label: this.$t('market.billType'), prop: 'billtype', width: '90' },
        { label: this.$t('market.billNo'), prop: 'frombillno', width: '90' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '120' },
        { label: this.$t('market.fmaterial'), type: 'order', prop: 'fnumber', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fname', width: '120' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification', width: '140' },
        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        { label: this.$t('market.stock2'), prop: 'fstockname' },
        { label: this.$t('market.supplier'), prop: 'fsupplier' },
        { label: this.$t('market.fqty'), prop: 'fqty', width: '80' },
        { label: this.$t('market.balbnPrice'), prop: 'fprice', width: '80' },
        { label: this.$t('market.balbnAmount'), prop: 'famount', width: '80' },
        { label: this.$t('market.fcategory'), prop: 'fcategory' },
        { label: this.$t('shop.fdescripTion'), prop: 'fdescription', width: '200' }
      ],
      api: '',
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '70vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryCostSubInstock({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '成本列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取成本数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const RES = await queryCostSubInstock(DATA)
      this.tableData = RES.data.array
      this.total = RES.data.total
      this.loading = false
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.currentPage = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetUntreated()
    }
  }
}
</script>
<style lang="scss" scoped>
.content1 {
  @include listBom;
  padding: 0;
  .el-form {
    .el-form-item {
      margin-bottom: 10px;
    }
  }
  .header{
    position:relative;
    margin-bottom: 5px;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
